package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.po.dataleveltyperel.DataLevelTypeRelPo;
import com.bwda.dsrs.filecenter.domain.condition.dataleveltyperel.DataLevelTypeRelCondition;
import com.bwda.dsrs.filecenter.domain.form.dataleveltyperel.DataLevelTypeRelQueryForm;
import com.bwda.dsrs.filecenter.domain.form.dataleveltyperel.DataLevelTypeRelUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.dataleveltyperel.DataLevelTypeRelCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.dataleveltyperel.DataLevelTypeRelVo;
import com.bwda.dsrs.filecenter.service.DataLevelTypeRelService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/dataleveltyperel")
@Api(value = "/dataleveltyperel", tags = "数据敏感级别和类别关系表")
public class DataLevelTypeRelController extends BaseController {

	@Autowired
	private DataLevelTypeRelService dataLevelTypeRelService;

	@ApiOperation(value = "查询数据敏感级别和类别关系表",notes = "查询数据敏感级别和类别关系表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DataLevelTypeRelVo> query(@ApiParam(value = "id", required = true)@RequestParam Integer id) throws BusinessServiceException {
		DataLevelTypeRelPo po = dataLevelTypeRelService.queryWithValid(id);
		DataLevelTypeRelVo vo = CopyUtil.transfer(po, DataLevelTypeRelVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询数据敏感级别和类别关系表数量",notes = "查询数据敏感级别和类别关系表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid DataLevelTypeRelQueryForm form) throws BusinessServiceException {
		DataLevelTypeRelCondition condition = this.getConditionByQueryForm(form);
		int count = dataLevelTypeRelService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询数据敏感级别和类别关系表列表",notes = "查询数据敏感级别和类别关系表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<DataLevelTypeRelVo>> queryList(@ModelAttribute@Valid DataLevelTypeRelQueryForm form) throws BusinessServiceException {
		DataLevelTypeRelCondition condition = this.getConditionByQueryForm(form);
		List<DataLevelTypeRelPo> poList = dataLevelTypeRelService.queryList(condition);
		List<DataLevelTypeRelVo> voList = CopyUtil.transfer(poList, DataLevelTypeRelVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询数据敏感级别和类别关系表列表(带分页)",notes = "查询数据敏感级别和类别关系表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<DataLevelTypeRelVo>> queryPageList(@ModelAttribute@Valid DataLevelTypeRelQueryForm form) throws BusinessServiceException {
		DataLevelTypeRelCondition condition = this.getConditionByQueryForm(form);
		List<DataLevelTypeRelVo> voList = new ArrayList<>();
		int count = dataLevelTypeRelService.queryCount(condition);
		if (count > 0) {
			List<DataLevelTypeRelPo> poList = dataLevelTypeRelService.queryList(condition);
			voList = CopyUtil.transfer(poList, DataLevelTypeRelVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增数据敏感级别和类别关系表",notes = "新增数据敏感级别和类别关系表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DataLevelTypeRelVo> add(@ModelAttribute@Valid DataLevelTypeRelCreateForm form) throws BusinessServiceException {
		DataLevelTypeRelPo po = CopyUtil.transfer(form, DataLevelTypeRelPo.class);
//		po.setId(RandomUtils.nextInt());
		dataLevelTypeRelService.insert(po);
		DataLevelTypeRelVo vo = CopyUtil.transfer(po, DataLevelTypeRelVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改数据敏感级别和类别关系表",notes = "修改数据敏感级别和类别关系表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid DataLevelTypeRelUpdateForm form) throws BusinessServiceException {
		DataLevelTypeRelPo po = CopyUtil.transfer(form, DataLevelTypeRelPo.class);
		dataLevelTypeRelService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除数据敏感级别和类别关系表",notes = "删除数据敏感级别和类别关系表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "id", required = true)@RequestParam Integer id) throws BusinessServiceException {
		dataLevelTypeRelService.delete(id);
		return getSuccessResult();
	}

	/**
	 * DataLevelTypeRelQueryForm转换为DataLevelTypeRelCondition
	 *
	 * @param form
	 * @return
	 */
	private DataLevelTypeRelCondition getConditionByQueryForm(DataLevelTypeRelQueryForm form) {
		DataLevelTypeRelCondition condition = CopyUtil.transfer(form, DataLevelTypeRelCondition.class);
		return condition;
	}

}